package edu.hawaii.myisern.action;


import java.util.Iterator;
import java.util.logging.Logger;

import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import edu.hawaii.myisern.collaborations.jaxb.Collaboration;
import edu.hawaii.myisern.model.MyIsernModel;
import edu.hawaii.myisern.organizations.jaxb.Organization;
import edu.hawaii.myisern.researchers.jaxb.Researcher;
import edu.hawaii.myisern.sanitycheck.SanityCheckErrorItem;

/**
 * Handles the Sanitiy Check items.
 * and Collaboration objects.
 * 
 * @author John Hauge
 *
 */
public class SanityCheckActionBean extends MyIsernActionBean {
  
  /** The singleton MyIsernModel. */
  private MyIsernModel myIsernModel = MyIsernModel.getInstance();
  private static Logger logger = Logger.getLogger("edu.hawaii.myisern.Hauge");
  private boolean checkResearchers;
  private boolean checkOrganizations;
  private boolean checkCollaborations;
  private String filter;
  
  /**
   * Constructor.
   */
  public SanityCheckActionBean() {
    this.checkResearchers = true;
    this.checkOrganizations = true;
    this.checkCollaborations = true;
    this.filter = "all";
  }
  /**
   * Provides an iterator over the Collaboration list to the page. 
   * @return The Collaboration iterator.
   */
  public Iterator<Collaboration> getCollaborationIterator() {
    return myIsernModel.cIterator();
  }
  /**
   * Gets the list of errors.
   * 
   * @return The report iterator.
   * 
   */
  public Iterator<SanityCheckErrorItem> getReportIterator() {
    try {
      return myIsernModel.getReportIterator();
    }
    catch (Exception e) {
      return null;
    }
  }
  /**
   * Researcher filter flag.
   * @return True/False
   */
  public boolean getCheckResearchers() {
    return this.checkResearchers;
  }
  /**
   * Organization filter flag.
   * @return True/False
   */
  public boolean getCheckOrganizations() {
    return this.checkOrganizations;
  }
  /**
   * Collaboration filter flag.
   * @return True/False
   */
  public boolean getCheckCollaborations() {
    return this.checkCollaborations;
  }
  /**
   * Provides an iterator over the Researcher list to the page. 
   * @return The Researcher iterator.
   */
  public Iterator<Researcher> getResearcherIterator() {
    return myIsernModel.rIterator();
  }
  
  /**
   * Provides an iterator over the Researcher list to the page. 
   * @return The Researcher iterator.
   */
  public Iterator<Organization> getOrganizationIterator() {
    return myIsernModel.oIterator();
  }
  
  /**
   * Displays the Sanity Check Report Page.
   * @return ForwardResolution to redisplay the scReport.jsp page
   */
  public Resolution sanityCheckReport() {
    return new ForwardResolution("/scReport.jsp");
  }
  /**
   * Updates the Sanity Check Report page with filter applied.
   * @return ForwardResolution to redisplay the scReport.jsp page
   */
  public Resolution filterReport() {
    return new ForwardResolution("/scReport.jsp");
  }
  /**
   * Returns Filter string.
   * @return filter string.
   */
  public String getFilterSetting() {
    return this.filter;
  }
  /**
   * Applies filtering for report.
   * @param filter Filter setting.
   */
  public void setFilterSetting(String filter) {
    this.filter = filter;
    if ("researchers".equals(filter)) {
      this.checkCollaborations = false;
      this.checkOrganizations = false;
      this.checkResearchers = true;
    }
    else if ("organizations".equals(filter)) {
      this.checkCollaborations = false;
      this.checkResearchers = false;
      this.checkOrganizations = true;
    }
    else if ("collaborations".equals(filter)) {
      this.checkOrganizations = false;
      this.checkResearchers = false;
      this.checkCollaborations = true;
    }
    else if ("all".equals(filter)) {
      this.checkCollaborations = true;
      this.checkOrganizations = true;
      this.checkResearchers = true;
    }
  }
}

